<?php
/**
 * Account page controller
 *
 * @author Dima
 *
 */

class AccountController extends modules_default_controllers_ControllerBase
{
	protected $_bForLoggedUsersOnly = true;

//----------------------------------------------------------------------------------------------------

	public function init()
	{
		parent::init();
		$this->view->title = 'Account';
	}

//----------------------------------------------------------------------------------------------------

	public function indexAction()
	{
		$allItems = models_PagecontentMapper::getItem('home');
		$this->view->allItems = $allItems ;
	}

//----------------------------------------------------------------------------------------------------

	public function myprofileAction()
	{
		$this->_redirect('/');
		$this->view->headScript()->appendFile('/js/jquery.validate.js');
		$this->view->headLink()->appendStylesheet('/css/validation_css/screen.css');
		$this->view->headScript()->appendFile('/js/profile.js');

		$aIdentity = Zend_Auth::getInstance()->getIdentity();
		$idUser = $aIdentity['User_id'];

		$aProfile 	  = models_UserMapper::fetchById($idUser);
		$allCountries = models_CountriesMapper::getAll();
		$allStates 	  = models_CountriesMapper::getAllStates();

		$this->view->allStates = $allStates;
		$this->view->allCountries = $allCountries;
		$this->view->aProfile = $aProfile;

		$oZendSession = Zend_Registry::getInstance()->get('Zend_Session_Namespace');
		$this->view->infoMessages = $oZendSession->infoMessages;
		$oZendSession->infoMessages = '';

	}

//----------------------------------------------------------------------------------------------------

	public function myreviewsAction()
	{
		$page = (int)$this->_getParam('page',1);
		$idUser = Lemar_User::getLoggedUserId();
		$reviews = models_ReviewsMapper::getUserReviews($page, 10, $idUser);
		$this->view->reviews = $reviews['paginator'];
	}

//----------------------------------------------------------------------------------------------------

	public function myordersAction()
	{
		$page = (int)$this->_getParam('page',1);
		$idUser = Lemar_User::getLoggedUserId();

		$orders = models_OrderMapper::getAll($page, 10, null, $idUser);
		$this->view->orders = $orders['paginator'];
	}

//----------------------------------------------------------------------------------------------------

	public function orderAction()
	{
		$idOrder = (int)$this->_getParam('id',0);
		$idUser = Lemar_User::getLoggedUserId();

		$order = models_OrderMapper::findById($idOrder);
		if (null == $order || $order->idUser != $idUser)
		{
			$this->_redirect('/');
		}

		$items = models_OrderMapper::getItems($idOrder);

		$this->view->items = $items;
		$this->view->order = $order;
	}

//----------------------------------------------------------------------------------------------------

	public function updateAction()
	{
		$this->_redirect('/');
		$oRequest = $this->getRequest();
		$oForm = new modules_default_forms_ProfileForm();

		if ($oRequest->isPost())
		{
			if ($oForm->isValid($oRequest->getParams()))
			{
				$aData = array();
				if($oRequest->getParam('email', null))
				{
					$aData['email']	= $oRequest->getParam('email');
				}
				if ($oRequest->getParam('new_password', null))
				{
					if ($oRequest->getParam('new_password') == $oRequest->getParam('re_enter_password'))
					{
						$aData['password']	= $oRequest->getParam('new_password');
					}
				}
				if ($oRequest->getParam('first_name', null))
				{
					$aData['first_name']	= $oRequest->getParam('first_name');
				}
				if ($oRequest->getParam('last_name', null))
				{
					$aData['last_name'] = $oRequest->getParam('last_name');
				}
				if ($oRequest->getParam('city', null))
				{
					$aData['city']		= $oRequest->getParam('city');
				}
				if ($oRequest->getParam('state', null))
				{
					$aData['state']		= $oRequest->getParam('state');
				}
				if ($oRequest->getParam('zip', null))
				{
					$aData['zip']		= $oRequest->getParam('zip');
				}
				if ($oRequest->getParam('country', null))
				{
					$aData['country'] 	= $oRequest->getParam('country');
				}
				if ($oRequest->getParam('profession_areas', null))
				{
					$aData['profession_areas'] 	= $oRequest->getParam('profession_areas');
				}
				if ($oRequest->getParam('profession', null))
				{
					$aData['profession'] 	= $oRequest->getParam('profession');
				}
				if ($oRequest->getParam('phone1', null))
				{
					if ($oRequest->getParam('phone2', null))
					{
						if ($oRequest->getParam('phone3', null))
						{
							$aData['phone'] = 	$oRequest->getParam('phone1') . '.' .
												$oRequest->getParam('phone2') . '.' .
												$oRequest->getParam('phone3');
						}
					}
				}


//					print_r($aData); die;

				$aIdentity = Zend_Auth::getInstance()->getIdentity();
				$idUser = $aIdentity['User_id'];
				if (models_UserMapper::updateArray($idUser, $aData))
				{
					$oZendSession = Zend_Registry::getInstance()->get('Zend_Session_Namespace');
					$oZendSession->infoMessages = 'Your information has been saved';
				}
				$this->_redirect('/profile/myprofile');
			}
			else
			{
				$aErrors = $oForm->getErrors();
				$aMessages = $oForm->getMessages();

//				print_r($aErrors);
//				print_r($aMessages);
			}
		}
	}



//----------------------------------------------------------------------------------------------------
}
